﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2010).                  *
*                                                                           *
****************************************************************************/
#endregion

#region Using directives
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System;
using System.Collections.Generic;
#endregion

namespace XNA_FRAMEWORK
{
	class xnaGameMgr : Microsoft.Xna.Framework.Game
	{
		#region Fields
		public static GraphicsDeviceManager graphics = null;
		protected static ContentManager content = null;
		private static float aspectRatio = 1.0f;
		XNAScreenMgr screenMgr;
		protected static int width = 0, height = 0;
		#endregion

		#region Initialization
		public xnaGameMgr()
		{
			Content.RootDirectory = "Content";
			graphics = new GraphicsDeviceManager(this);
			graphics.PreferredBackBufferWidth = 750;
			graphics.PreferredBackBufferHeight = 500;
			//graphics.ToggleFullScreen();
			//TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 60);

			screenMgr = new XNAScreenMgr(this);
			Components.Add(screenMgr);
			screenMgr.AddScreen(new XNABackgroundScreen(), null);
			screenMgr.AddScreen(new XNAMainMenuScreen(), null);
		}

		protected override void Initialize()
		{
			graphics.SynchronizeWithVerticalRetrace = false;
			graphics.ApplyChanges();
			IsFixedTimeStep = false;
			base.Initialize();
		}

		static public GraphicsDevice Device
		{
			get
			{
				return graphics.GraphicsDevice;
			}
		}

		public new static ContentManager ContentXNA
		{
			get
			{
				return content;
			}
		}

		public static int Width
		{
			get
			{
				return width;
			}
		}

		public static int Height
		{
			get
			{
				return height;
			}
		}

		public static float AspectRatio
		{
			get
			{
				return aspectRatio;
			}
		}

		public static Rectangle ResolutionRect
		{
			get
			{
				return new Rectangle(0, 0, width, height);
			}
		}

		protected override void LoadContent()
		{
			base.LoadContent();
		}

		protected override void Update(GameTime gameTime)
		{
			float fps = (1000.0f / (float)gameTime.ElapsedRealTime.TotalMilliseconds);
			this.Window.Title = "FPS: " + fps.ToString();
			base.Update(gameTime);
		}
		#endregion

		#region Draw
		protected override void Draw(GameTime gameTime)
		{
			//this.Window.Title = "XNA Game";
			this.IsMouseVisible = true;
			graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
			base.Draw(gameTime);
		}
		#endregion

		public static Rectangle CalcRectangle(
			int relX, int relY, int relWidth, int relHeight)
		{
			float widthFactor = width / 1024.0f;
			float heightFactor = height / 640.0f;
			return new Rectangle(
				(int)Math.Round(relX * widthFactor),
				(int)Math.Round(relY * heightFactor),
				(int)Math.Round(relWidth * widthFactor),
				(int)Math.Round(relHeight * heightFactor));
		}

		public static Rectangle CalcRectangleWithBounce(
			int relX, int relY, int relWidth, int relHeight, float bounceEffect)
		{
			float widthFactor = width / 1024.0f;
			float heightFactor = height / 640.0f;
			float middleX = (relX + relWidth / 2) * widthFactor;
			float middleY = (relY + relHeight / 2) * heightFactor;
			float retWidth = relWidth * widthFactor * bounceEffect;
			float retHeight = relHeight * heightFactor * bounceEffect;
			return new Rectangle(
				(int)Math.Round(middleX - retWidth / 2),
				(int)Math.Round(middleY - retHeight / 2),
				(int)Math.Round(retWidth),
				(int)Math.Round(retHeight));
		}

		public static Rectangle CalcRectangleKeep4To3(
			int relX, int relY, int relWidth, int relHeight)
		{
			float widthFactor = width / 1024.0f;
			float heightFactor = height / 768.0f;
			return new Rectangle(
				(int)Math.Round(relX * widthFactor),
				(int)Math.Round(relY * heightFactor),
				(int)Math.Round(relWidth * widthFactor),
				(int)Math.Round(relHeight * heightFactor));
		}

		public static Rectangle CalcRectangleKeep4To3(
			Rectangle gfxRect)
		{
			float widthFactor = width / 1024.0f;
			float heightFactor = height / 768.0f;
			return new Rectangle(
				(int)Math.Round(gfxRect.X * widthFactor),
				(int)Math.Round(gfxRect.Y * heightFactor),
				(int)Math.Round(gfxRect.Width * widthFactor),
				(int)Math.Round(gfxRect.Height * heightFactor));
		}

		public static Rectangle CalcRectangle1600(
			int relX, int relY, int relWidth, int relHeight)
		{
			float widthFactor = width / 1600.0f;

			float heightFactor = (height / 1200.0f);// / (aspectRatio / (16 / 9));
			return new Rectangle(
				(int)Math.Round(relX * widthFactor),
				(int)Math.Round(relY * heightFactor),
				(int)Math.Round(relWidth * widthFactor),
				(int)Math.Round(relHeight * heightFactor));
		}

		public static Rectangle CalcRectangle2000(
			int relX, int relY, int relWidth, int relHeight)
		{
			float widthFactor = width / 2000.0f;
			float heightFactor = (height / 1500.0f);
			return new Rectangle(
				(int)Math.Round(relX * widthFactor),
				(int)Math.Round(relY * heightFactor),
				(int)Math.Round(relWidth * widthFactor),
				(int)Math.Round(relHeight * heightFactor));
		}

		public static Rectangle CalcRectangleKeep4To3AlignBottom(
			int relX, int relY, int relWidth, int relHeight)
		{
			float widthFactor = width / 1024.0f;
			float heightFactor16To9 = height / 640.0f;
			float heightFactor4To3 = height / 768.0f;
			return new Rectangle(
				(int)(relX * widthFactor),
				(int)(relY * heightFactor16To9) -
				(int)Math.Round(relHeight * heightFactor4To3),
				(int)Math.Round(relWidth * widthFactor),
				(int)Math.Round(relHeight * heightFactor4To3));
		}

	}
}